# vim:ft=gdb:ts=2:sw=2:et:

set $UMF_AUDIT = 1
set $UMF_DEADBEEF = 2
set $UMF_REDZONE = 4
set $UMF_CONTENTS = 8
set $UMF_CHECKSIGNAL = 16
set $UMF_NOMAGAZINE = 32
set $UMF_FIREWALL = 64
set $UMF_LITE = 128
set $UMF_HASH = 256
set $UMF_RANDOMIZE = 512

define umastat_cache
  set $cp = $arg0
  set $meminuse = ($cp->cache_slab_create - $cp->cache_slab_destroy) * $cp->cache_slabsize

  if (($cp->cache_cpu[0].cc_magsize != 0) || ($cp->cache_flags & $UMF_NOMAGAZINE))
    set $magsize = $cp->cache_cpu[0].cc_magsize
  else
    set $magsize = $cp->cache_magtype->mt_magsize
  end
  
  set $alloc = $cp->cache_slab_alloc + $cp->cache_full.ml_alloc
  set $avail = $cp->cache_full.ml_total * $magsize
  set $total = $cp->cache_buftotal

  # walk the cpu caches
  set $cpu_current = 0
  while $cpu_current < umem_max_ncpus
    set $cpu = umem_cpus[$cpu_current]
    set $cpu_cache = (umem_cpu_cache_t*)((char*)$cp + $cpu->cpu_cache_offset)

    if $cpu_cache->cc_rounds > 0
      set $avail = $avail + $cpu_cache->cc_rounds
    end
    if $cpu_cache->cc_prounds > 0
      set $avail = $avail + $cpu_cache->cc_prounds
    end
    set $alloc = $alloc + $cpu_cache->cc_alloc
    
    set $cpu_current = $cpu_current + 1
  end

  # walk some slabs
  set $slab = $cp->cache_nullslab.slab_next
  while $slab != &$cp->cache_nullslab
    set $avail = $avail + $slab->slab_chunks - $slab->slab_refcnt
    set $slab = $slab->slab_next 
  end

  printf "%-25s    %6u %6u %6u %9u %9u %5llu\n", $cp->cache_name, $cp->cache_bufsize, $total - $avail, $total, $meminuse, $alloc, $cp->cache_alloc_fail
end

document umastat
  umem allocator stats
end

define umastat
  printf "cache                        buf    buf    buf    memory        alloc alloc\n"
  printf "name                         size   in use total  in use      succeed  fail\n"
  printf "---------------------------- ------ ------ ------ --------- --------- -----\n"
  set $cp = umem_null_cache->cache_next
  while $cp != &umem_null_cache
    umastat_cache $cp
    set $cp = $cp->cache_next
  end
  printf "---------------------------- ------ ------ ------ --------- --------- -----\n"
  set $vmem = vmem_list
  while $vmem != 0
    set $meminuse = 0
    set $alloc = 0
    set $fail = 0
    set $cp = umem_null_cache->cache_next
    while $cp != &umem_null_cache
      if $cp->cache_arena == $vmem
        set $meminuse = $meminuse + (($cp->cache_slab_create - $cp->cache_slab_destroy) * $cp->cache_slabsize)

        if (($cp->cache_cpu[0].cc_magsize != 0) || ($cp->cache_flags & $UMF_NOMAGAZINE))
          set $magsize = $cp->cache_cpu[0].cc_magsize
        else
          set $magsize = $cp->cache_magtype->mt_magsize
        end
  
        set $alloc = $alloc + ($cp->cache_slab_alloc + $cp->cache_full.ml_alloc)
        set $avail = $avail + ($cp->cache_full.ml_total * $magsize)
      end
      set $cp = $cp->cache_next
    end
    printf "Total %-25s                   %9u %9u %5u\n", $vmem->vm_name, $meminuse, $alloc, $fail 
    set $vmem = $vmem->vm_next
  end
end

define umem_status
  printf "Concurrency:\t%d\n", umem_max_ncpus 
  printf "Logs\t\t"
  if umem_transaction_log != 0
    printf "transaction=%d ", umem_transaction_log->lh_chunksize * umem_transaction_log->lh_nchunks
  end
  if umem_content_log != 0
    printf "content=%d ", umem_content_log->lh_chunksize * umem_content_log->lh_nchunks
  end
  if umem_failure_log != 0
    printf "fail=%d ", umem_failure_log->lh_chunksize * umem_failure_log->lh_nchunks
  end
  if umem_slab_log != 0
    printf "slab=%d ", umem_slab_log->lh_chunksize * umem_slab_log->lh_nchunks
  end
  if umem_logging == 0
    printf "(inactive)"
  end
  printf "\nMessage buffer:\n"
  set $msg = &umem_error_buffer + umem_error_begin
  printf "%s", $msg
end

